package com.sx.zongzhi.adapter;


import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.BusinessBean;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.List;

/**
 * Created by XZY on 2017/12/4.
 * 业务信息选择的adapter
 */

public class BusinessSelectAdapter extends CommonAdapter<BusinessBean.SubTypesBean> {


    private List<String> selectList;
    /**是否可以选择*/
    private boolean isSelect=false;
    BusinessCallback mCallback;

    public BusinessSelectAdapter(Context context, int layoutId, List<BusinessBean.SubTypesBean> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder viewHolder, final BusinessBean.SubTypesBean item, int position) {
        CheckBox checkBox=viewHolder.getView(R.id.checkbox);
        checkBox.setClickable(isSelect);
        checkBox.setText(item.getSubTypeCname());
        if (selectList!=null) {
            for (String str : selectList) {
                if (str.equals(item.getSubTypeKeyName())) {
                    checkBox.setChecked(true);
                    break;
                }
            }
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                   if (mCallback!=null){
                       mCallback.addBusiness(item.getSubTypeKeyName());
                   }
                }else {
                    if (mCallback!=null){
                        mCallback.deleteBusiness(item.getSubTypeKeyName());
                    }
                }
            }
        });
    }

    /**
     * 定义一个接口
     */
    public interface BusinessCallback {
        void addBusiness(String str);
        void deleteBusiness(String str);
    }

    /*接口回调*/
    public void getBusiness(BusinessCallback callback) {
        mCallback = callback;
    }

    public void selectData(List<String> lists){
        selectList=lists;
    }

    public void setSelected(boolean isSelect){
        this.isSelect=isSelect;
    }

}
